#!/usr/bin/env python
# coding=utf-8
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util, RequestHandler
import time
from model import get_member_by_id
from location import memb_latest_modify_time
from google.appengine.api import memcache
import logging

SAVE_LOCATION_BEATTIM = 300
ACTIVE_BEATTIM = 6

#"每五分钟保存一次用户的坐标
class SaveLocation(RequestHandler):
    def get(self):
        pass
#        for (key,value) in memb_latest_modify_time.items():
#            if time.time() - value >= SAVE_LOCATION_BEATTIM:
#                member = get_member_by_id(key)
#                logging.log(0, member.username+"'s location saved")
#                member.save_location()

class CheckOffLineUsers(RequestHandler):
    def get(self):
        for (key,value) in memb_latest_modify_time.items():
            if time.time() - value >= ACTIVE_BEATTIM:
                member = get_member_by_id(key)
                logging.log(0, member.username+" signout")
                member.signout()
                
class Active(RequestHandler):
    def get(self):
        for (key,value) in memb_latest_modify_time.items():
            if time.time() - value >= ACTIVE_BEATTIM:
                del memb_latest_modify_time[key]

def main():
    application = webapp.WSGIApplication([('/tasks/savelocation',SaveLocation),
                                          ('/tasks/checkofflineusers',CheckOffLineUsers)
                                          ],
                                         debug=True)
    util.run_wsgi_app(application)
    
if __name__ == "__main__":
    main()
